﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OUSMS.AppLogic.DataAccessLayer;

namespace OUSMS.AppLogic.BusinessLogicLayer
{
    [System.ComponentModel.DataObject(true)]
    public class MotionsBLL : IDisposable
    {
        DatabaseContext _DatabaseContext;
        public MotionsBLL():this(new DatabaseContext())
        { 
        }

        public MotionsBLL(DatabaseContext DatabaseContext)
        {
            _DatabaseContext = DatabaseContext;
        }

        public void Dispose()
        {
            if (_DatabaseContext != null)
            {
                _DatabaseContext.Dispose();
                _DatabaseContext = null;
            }

            GC.SuppressFinalize(this);
        }

        public Motions CreateMotion(Motions motion)
        {
            motion = _DatabaseContext.Motions.Add(motion);
            _DatabaseContext.SaveChanges();
            return motion;
        }

        public Motions GetMotionsByID(long motionID)
        {
            return _DatabaseContext.Motions.Where(m => m.ID == motionID).FirstOrDefault();
        }

        public IQueryable<Motions> GetMotionsByProserID(string loginName)
        {
            return _DatabaseContext.Motions.Where(m => m.Users.LoginName.Equals(loginName) && !string.IsNullOrEmpty(m.Title) && !m.IsDeleted);
        }
        
        public IQueryable<Motions> GetHaveDoneMotionList(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<Motions> motions = _DatabaseContext.Motions.Where(m => m.Status.Equals("审核结束"));
            totalRowCount = motions.Count();
            return motions.OrderBy(m => m.Status).Skip(startRowIndex).Take(maximumRows);
        }

        public IQueryable<Motions> GetWaitForPrejudicMotionList(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<Motions> motions = _DatabaseContext.Motions.Where(m => m.Status.Equals("待预审"));
            totalRowCount = motions.Count();
            return motions.OrderBy(m => m.SumbitDateTime).Skip(startRowIndex).Take(maximumRows);
        }

        public IQueryable<Motions> GetWaitForTransferMotionList(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<Motions> motions = _DatabaseContext.Motions.Where(m => m.Status.Equals("待转发"));
            totalRowCount = motions.Count();
            return motions.OrderBy(m => m.PrejudicDateTime).Skip(startRowIndex).Take(maximumRows);
        }

        public IQueryable<Motions> GetOKMotionList(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<Motions> motions = _DatabaseContext.Motions.Where(m => m.Status.Equals("已立项"));
            totalRowCount = motions.Count();
            return motions.OrderByDescending(m => m.EndDateTime).Skip(startRowIndex).Take(maximumRows);
        }

        public IQueryable<Motions> GetNotOKMotionList(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<Motions> motions = _DatabaseContext.Motions.Where(m => m.Status.Equals("不立项"));
            totalRowCount = motions.Count();
            return motions.OrderByDescending(m => m.EndDateTime).Skip(startRowIndex).Take(maximumRows);
        }

        public IQueryable<Motions> GetMotionHistoryList(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression, string roleType, long userID)
        {
            IQueryable<Motions> motions = _DatabaseContext.Motions.Where(m => m.MotionReviewerComments.Where(r=>r.ReviewerID == userID && r.RoleType.Equals(roleType) && r.CommentDateTime.HasValue).Count()>0);
            totalRowCount = motions.Count();
            return motions.OrderByDescending(m => m.EndDateTime).Skip(startRowIndex).Take(maximumRows);
        }

        public IQueryable<Motions> GetWaitForEndMotionList(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<Motions> motions = _DatabaseContext.Motions.Where(m => m.Status.Equals("待立项"));
            totalRowCount = motions.Count();
            return motions.OrderBy(m => m.TransferDateTime).Skip(startRowIndex).Take(maximumRows);
        }

        public string GetMotionFeedbackProgress(long motionID)
        {
            int totalCount = _DatabaseContext.MotionReviewerComments.Where(m => m.MotionID == motionID).Count();
            int giveCount = _DatabaseContext.MotionReviewerComments.Where(m => m.MotionID == motionID && m.CommentDateTime.HasValue).Count();
            return giveCount + "/" + totalCount;
        }

        public int GetTotalNeedFeedbackCount(long motionID)
        {
            return _DatabaseContext.MotionReviewerComments.Where(m => m.MotionID == motionID).Count();
        }

        public int GetTotalGiveFeedbackCount(long motionID)
        {
            return _DatabaseContext.MotionReviewerComments.Where(m => m.MotionID == motionID && m.CommentDateTime.HasValue).Count();
        }

        public IQueryable<Motions> GetWaitForExpertFeedbackMotionListByExpertID(long expertID)
        {
            return _DatabaseContext.Motions.Where(m => m.Status.Equals("待立项") && m.MotionReviewerComments.Count(mc => mc.ReviewerID == expertID && mc.RoleType.Equals("评审专家") && !mc.CommentDateTime.HasValue) > 0).OrderBy(m => m.TransferDateTime);
        }

        public IQueryable<Motions> GetWaitForDepartmentLeaderFeedbackMotionListByExpertID(long expertID)
        {
            return _DatabaseContext.Motions.Where(m => m.Status.Equals("待立项") && m.MotionReviewerComments.Count(mc => mc.ReviewerID == expertID && mc.RoleType.Equals("部门负责人") && !mc.CommentDateTime.HasValue) > 0).OrderBy(m => m.TransferDateTime);
        }

        public IQueryable<Motions> GetWaitForSchoolLeaderFeedbackMotionListByExpertID(long expertID)
        {
            return _DatabaseContext.Motions.Where(m => m.Status.Equals("待立项") && m.MotionReviewerComments.Count(mc => mc.ReviewerID == expertID && mc.RoleType.Equals("校领导") && !mc.CommentDateTime.HasValue) > 0).OrderBy(m => m.TransferDateTime);
        }

        public void Save()
        {
            _DatabaseContext.SaveChanges();
        }
    }
}